﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class UpdateProductsCategoryForm : Form
    {
        private ProductsBLL _productsBLL = new ProductsBLL();
        private ProductsCategory _category = null;
        public UpdateProductsCategoryForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }


        //订阅事件的方法
        public void GetValue(object sender, EventArgs e)
        {
            _category = e as ProductsCategory;
        }

        private void UpdateProductsCategoryForm_Load(object sender, EventArgs e)
        {
            if (_category == null)      //添加
            {
                button1.Text = "添加";
            }
            else
            {
                button1.Text = "修改";
                txtCid.Text = _category.CatId.ToString();
                txtCname.Text = _category.CatName.ToString();
                txtDesc.Text = _category.Remark.ToString();
                registDate.Text = _category.SubTime.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(_category == null)   //添加
            {
                ProductsCategory productsCategory = new ProductsCategory();
                productsCategory.CatName = txtCname.Text;
                productsCategory.Remark = txtDesc.Text;
                productsCategory.SubTime = registDate.Value;

                if(_productsBLL.UpdateCategory(productsCategory, 0) > 0)
                {
                    MessageBox.Show("添加成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                ProductsCategory productsCategory = new ProductsCategory();
                productsCategory.CatId = Convert.ToInt32(txtCid.Text);
                productsCategory.CatName = txtCname.Text;
                productsCategory.Remark = txtDesc.Text;
                productsCategory.SubTime = registDate.Value;

                if(_productsBLL.UpdateCategory(productsCategory, 1) > 0)
                {
                    MessageBox.Show("修改成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }

            
        }
    }
}
